<?php
global $SITEURL;
$mld = MLDnewsletter::getInstance();
$letterid = $_GET['viewletter'];
$xml = $mld->getXmlData('newsletters');
$url = $_SERVER['SCRIPT_NAME'] . '?id=mld-newsletter&viewletter=' . $letterid;

foreach ($xml as $letter) {
    if ((string) $letter->id == $letterid) {
        $lettertitle = $letter->title;
        $lettercontent = $letter->content;
        $alreadysend = $letter->send;
        break;
    }
}
$sxml = $mld->getXmlData('settings');
$theader = $sxml->theader;
$tfooter = $sxml->tfooter;
//$letterhtml = htmldecode($theader.$lettercontent.$tfooter);
$letterhtml = stripslashes(htmldecode($theader . $lettercontent . $tfooter));
$senderEmail = $sxml->semail;
$senderName = $sxml->sname;

if (isset($_GET['testsend'])) {
    //grab site email if needed
    if($sxml->usesitemail == 'true') {
        $semail = '';
        if (isset($_COOKIE['GS_ADMIN_USERNAME'])) {
            $cookie_user_id = _id($_COOKIE['GS_ADMIN_USERNAME']);
            if (file_exists(GSUSERSPATH . $cookie_user_id.'.xml')) {
                $datau = getXML(GSUSERSPATH  . $cookie_user_id.'.xml');
                $USR = stripslashes($datau->USR);
                $file = _id($USR) .'.xml';
                $data  = getXML(GSUSERSPATH . $file);
                $semail =(string) $data->EMAIL;
            }
        }
        if($semail == '') {                
            display_message('error', i18n_r('mld-newsletter/SITEMAILNOTFOUND'));
            break 3;
        }
    } else {            
        $semail = $senderEmail;
    }
    $result = send_email($semail, $senderName, $lettertitle, $letterhtml);
    display_message($result[0], $result[1]);
}
if (isset($_GET['sendindeed'])) {

    $subscriberslist = array();
    $subscribersxml = getXML(GSDATAOTHERPATH . 'mld-newsletter/mld-newsletter-subscribers.xml');
    foreach ($subscribersxml as $subscriber) {
        $subscriberslist[(string) $subscriber->email] = $subscriber->name;
    }

    $result = send_email($subscriberslist, "", $lettertitle, $letterhtml, true);
    display_message($result[0], $result[1]);

    if ($result[0] == 'updated') {
        $xml = $mld->getXmlData('newsletters');
        foreach ($xml as $entry) {
            if ((string) $entry->id == $letterid) {
                $entry->send = 'true';
                XMLsave($xml, $mld->files['newsletters']);
            }
        }
    }
}
?>

<h3><?php i18n('mld-newsletter/VIEWLETTER'); ?>: <?php echo $lettertitle; ?></h3>
<div class="edit-nav mldeditnav">
    <p>
        <a href="<?php echo $url.'&testsend'; ?>"><?php i18n('mld-newsletter/TESTSEND'); ?></a>
    <?php if($alreadysend == 'true'): ?>
        <p class="mldalreadysendmessage"><?php i18n('mld-newsletter/BUTTONSENDHIDE'); ?></p>    
    <?php else: ?>
        <a href="#" id="send_button"><?php i18n('mld-newsletter/SEND'); ?></a>
    <?php endif; ?>
    </p>
</div>
<p id="sendindeed_warning" class="mldwarning" style="display:none;"><?php i18n('mld-newsletter/SENDWARNING'); ?>
    <?php if(GSVERSION > 4.0): ?>
    <a href="#" class="cancel" onclick="mldSendNewsletters('<?php echo $SITEURL; ?>')"><?php i18n('YES'); ?></a>
    <?php else: ?>
        <a href="<?php echo $url; ?>&sendindeed')" class="cancel"><?php i18n('YES'); ?></a>
    <?php endif; ?>    
    <a href="#" class="cancel" id="cancelsend_button"><?php i18n('NO'); ?></a>
</p>
<script type="text/javascript">
    $('#send_button').live("click", function($e) {
        $e.preventDefault();
        $("#sendindeed_warning").show();        
    });
    $("#cancelsend_button").live("click", function($e) {
        $e.preventDefault();
        $("#sendindeed_warning").hide();        
    });
</script>

<div id="mldpreview">
<?php echo $letterhtml; ?>
</div>            